
package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.AgencyListItem;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class OfferHistoryViewBinder
        extends ItemViewBinder<AgencyListItem, OfferHistoryViewBinder.ViewHolder> implements View.OnClickListener {


    public interface OnBuyerListener {
        void onBuyer(View view, int position, AgencyListItem data);
    }

    View.OnClickListener onRecommendListener;
    OnBuyerListener onBuyerListener;
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public View.OnClickListener getOnRecommendListener() {
        return onRecommendListener;
    }

    public void setOnRecommendListener(View.OnClickListener onRecommendListener) {
        this.onRecommendListener = onRecommendListener;
    }

    public OnBuyerListener getOnBuyerListener() {
        return onBuyerListener;
    }

    public void setOnBuyerListener(OnBuyerListener onBuyerListener) {
        this.onBuyerListener = onBuyerListener;
    }

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_agence_list, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull AgencyListItem post) {

      /*  holder.rl_base.setTag(R.id.item_key_position, getPosition(holder));
        holder.rl_base.setTag(R.id.item_key_status, post);
        holder.rl_base.setOnClickListener(this);
        holder.rl_product.setTag(R.id.item_key_position, getPosition(holder));
        holder.rl_product.setTag(R.id.item_key_status, post);
        holder.rl_product.setOnClickListener(this);

        holder.tv_product_new.setTag(R.id.item_key_position, getPosition(holder));
        holder.tv_product_new.setTag(R.id.item_key_status, post);
        holder.tv_product_new.setOnClickListener(this);
*/

        holder.setData(post);
        holder.itemView.setTag(R.id.item_key_position, getPosition(holder));
        holder.itemView.setTag(R.id.item_key_status, post);
        holder.itemView.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);


            switch (v.getId()) {
                case R.id.tv_product_new:

                    if (onBuyerListener != null && o instanceof AgencyListItem) {
                        onBuyerListener.onBuyer(v, position, (AgencyListItem) o);
                    }
                    break;

                default:
                    if (onRecyclerViewItemClickListener != null && o instanceof AgencyListItem) {
                        onRecyclerViewItemClickListener.onItemClick(v, position, o);
                    }
                    break;
            }


        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvSelectAgentName;
        TextView tvSelectAgentAddress;
        ImageView tvSelectAgentMap;
        TextView tvSelectStoreName;


        ViewHolder(@NonNull View itemView) {
            super(itemView);


            tvSelectAgentName = itemView.findViewById(R.id.tv_select_angent_name);
            tvSelectAgentAddress = itemView.findViewById(R.id.tv_select_address);
            tvSelectAgentMap = itemView.findViewById(R.id.iv_select_agent_map);
            tvSelectStoreName = itemView.findViewById(R.id.tv_select_store_name);



        }


        void setData(AgencyListItem post) {

            tvSelectAgentName.setText(post.getConsigneeName() + "  "+post.getConsigneePhone());
            tvSelectAgentAddress.setText(post.getConsigneeAddress());
            tvSelectStoreName.setText(post.getName());

        }


    }
}
